package com.aco.party.service.system;

import com.aco.party.dao.custom.EventsDao;
import com.aco.party.enums.VisitTypeEnum;
import com.aco.party.model.po.Events;
import com.aco.party.model.vo.EventVo;
import com.aco.party.model.vo.VisitParam;
import com.aco.party.model.vo.common.PageParam;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by WangShijun on 2016/3/3.
 */
@Service
public class EventService {
    @Resource
    private EventsDao eventsDao;

    @Resource
    private VisitService visitService;

    public List<EventVo> getEventsByPage(PageParam pageParam){
        List<EventVo> list = new ArrayList<>();
        List<Events> eventsList = eventsDao.getEvents(pageParam);
        EventVo vo;
        if(eventsList != null){
            for (Events events : eventsList){
                vo = new EventVo();
                BeanUtils.copyProperties(events,vo);
                VisitParam visitParam = new VisitParam();
                visitParam.setType(VisitTypeEnum.活动.getValue());
                visitParam.setTargetId(vo.getEventId());
                vo.setViews(visitService.getCountByParam(visitParam).longValue());
                list.add(vo);
            }
        }
        return list;
    }

    public Integer getEventsTotal(){
        return eventsDao.getEventsTotal();
    }

    public void update(Events events){
        events.setUpdateTime(new Date());
        eventsDao.updateByPrimaryKeySelective(events);
    }

    public void add(Events events){
        events.setCreateTime(new Date());
        eventsDao.insertSelective(events);
    }

    public void delete(Long id){
        eventsDao.deleteByPrimaryKey(id);
    }

    public Events getByPrimaryKey(Long id){
        return eventsDao.selectByPrimaryKey(id);
    }
}
